home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / dskut / aspisrc.zip / LIST.C < prev    next >
C/C++ Source or Header  |  1992-01-26  |  17KB  |  722 lines

  1. /* List a tar archive.
  2.    Copyright (C) 1988 Free Software Foundation
  3.  
  4. This file is part of GNU Tar.
  5.  
  6. GNU Tar is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. GNU Tar is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU Tar; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. /*
  21.  * List a tar archive.
  22.  *
  23.  * Also includes support routines for reading a tar archive.
  24.  *
  25.  * this version written 26 Aug 1985 by John Gilmore (ihnp4!hoptoad!gnu).
  26.  *
  27.  * @(#)list.c 1.31 11/5/87 - gnu
  28.  */
  29. #include <stdio.h>
  30. #include <ctype.h>
  31. #include <sys/types.h>
  32. #include <sys/stat.h>
  33. #ifndef    MSDOS
  34. #include <sys/file.h>
  35. #endif    /* MSDOS */
  36.  
  37. #ifdef USG
  38. #include <sys/sysmacros.h>    /* major() and minor() defined here */
  39. #endif
  40.  
  41. char *ctime();                /* From libc.a */
  42.  
  43. #define    isodigit(c)    ( ((c) >= '0') && ((c) <= '7') )
  44.  
  45. #include "tar.h"
  46. #include "port.h"
  47.  
  48. extern FILE *msg_file;
  49.  
  50. long from_oct();            /* Decode octal number */
  51. void demode();                /* Print file mode */
  52.  
  53. union record *head;            /* Points to current archive header */
  54. struct stat hstat;            /* Stat struct corresponding */
  55. int head_standard;            /* Tape header is in ANSI format */
  56.  
  57. void print_header();
  58. void skip_file();
  59. void skip_extended_headers();
  60.  
  61. extern char *quote_copy_string();
  62.  
  63.  
  64. /*
  65.  * Main loop for reading an archive.
  66.  */
  67. void
  68. read_and(do_something)
  69.     void (*do_something)();
  70. {
  71.     int status = 3;            /* Initial status at start of archive */
  72.     int prev_status;
  73.     extern time_t new_time;
  74.     char save_linkflag;
  75.     
  76.     name_gather();            /* Gather all the names */
  77.     open_archive(1);        /* Open for reading */
  78.  
  79.     for(;;) {
  80.         prev_status = status;
  81.         status = read_header();
  82.         switch (status) {
  83.  
  84.         case 1:            /* Valid header */
  85.             /* We should decode next field (mode) first... */
  86.             /* Ensure incoming names are null terminated. */
  87.             head->header.name[NAMSIZ-1] = '\0';
  88.  
  89.             if (   !name_match(head->header.name)
  90.                  || (f_new_files && hstat.st_mtime<new_time)
  91.                  || (f_exclude && check_exclude(head->header.name))) {
  92.  
  93.                 int isextended = 0;
  94.  
  95.                 if(   head->header.linkflag==LF_VOLHDR
  96.                     || head->header.linkflag==LF_MULTIVOL
  97.                    || head->header.linkflag==LF_NAMES) {
  98.                     (*do_something)();
  99.                     continue;
  100.                 }
  101.                 /* Skip past it in the archive */
  102.                 if (head->header.isextended)
  103.                     isextended = 1;
  104.                 save_linkflag = head->header.linkflag;
  105.                 userec(head);
  106.                 if (isextended) {
  107. /*                    register union record *exhdr;
  108.  
  109.                     for (;;) {
  110.                         exhdr = findrec();
  111.                         if (!exhdr->ext_hdr.isextended) {
  112.                             userec(exhdr);
  113.                             break;
  114.                         }
  115.                     }
  116.                     userec(exhdr);*/
  117.                     skip_extended_headers();
  118.                 }
  119.                 /* Skip to the next header on the archive */
  120.                 if(save_linkflag != LF_DIR)
  121.                     skip_file((long)hstat.st_size);
  122.                 continue;
  123.  
  124.             }
  125.  
  126.             (*do_something)();
  127.             continue;
  128.  
  129.             /*
  130.              * If the previous header was good, tell them
  131.              * that we are skipping bad ones.
  132.              */
  133.         case 0:            /* Invalid header */
  134.             userec(head);
  135.             switch (prev_status) {
  136.             case 3:        /* Error on first record */
  137.                 msg("Hmm, this doesn't look like a tar archive.");
  138.                 /* FALL THRU */
  139.             case 2:        /* Error after record of zeroes */
  140.             case 1:        /* Error after header rec */
  141.                 msg("Skipping to next file header...");
  142.             case 0:        /* Error after error */
  143.                 break;
  144.             }
  145.             continue;
  146.  
  147.         case 2:            /* Record of zeroes */
  148.             userec(head);
  149.             status = prev_status;    /* If error after 0's */
  150.             if (f_ignorez)    
  151.                 continue;
  152.             /* FALL THRU */
  153.         case EOF:        /* End of archive */
  154.             break;
  155.         }
  156.         break;
  157.     };
  158.  
  159.     close_archive();
  160.     names_notfound();        /* Print names not found */
  161. }        
  162.  
  163.  
  164. /*
  165.  * Print a header record, based on tar options.
  166.  */
  167. void
  168. list_archive()
  169. {
  170.     extern char *save_name;
  171.     int    isextended = 0; /* Flag to remember if head is extended */
  172.     
  173.     /* Save the record */
  174.     saverec(&head);
  175.  
  176.     /* Print the header record */
  177.     if (f_verbose) {
  178.         if (f_verbose > 1)
  179.             decode_header(head, &hstat, &head_standard, 0);
  180.         print_header();
  181.     }
  182.  
  183.     if(f_gnudump && head->header.linkflag==LF_DUMPDIR) {
  184.         size_t    size, written, check;
  185.         char    *data;
  186.         extern int errno;
  187.         extern long save_totsize;
  188.         extern long save_sizeleft;
  189.  
  190.         userec(head);
  191.         if(f_multivol) {
  192.             save_name = head->header.name;
  193.             save_totsize=hstat.st_size;
  194.         }
  195.         for(size = hstat.st_size;size>0;size-=written) {
  196.             if(f_multivol)
  197.                 save_sizeleft=size;
  198.             data = findrec()->charptr;
  199.             if(data==NULL) {
  200.                 msg("EOF in archive file?");
  201.                 break;
  202.             }
  203.             written = endofrecs()->charptr - data;
  204.             if(written>size)
  205.                 written=size;
  206.             errno=0;
  207.             check=fwrite(data,sizeof(char), written, msg_file);
  208.             userec((union record *)(data+written - 1));
  209.             if(check!=written) {
  210.                 msg_perror("only wrote %ld of %ld bytes to file %s",check, written,head->header.name);
  211.                 skip_file((long)(size)-written);
  212.                 break;
  213.             }
  214.         }
  215.         if(f_multivol)
  216.             save_name = 0;
  217.         saverec((union record **) 0);    /* Unsave it */
  218.         fputc('\n',msg_file);
  219.         fflush(msg_file);
  220.         return;
  221.  
  222.     }
  223.     saverec((union record **) 0);    /* Unsave it */
  224.     /* Check to see if we have an extended header to skip over also */
  225.     if (head->header.isextended) 
  226.         isextended = 1;
  227.         
  228.     /* Skip past the header in the archive */
  229.     userec(head);
  230.  
  231.     /*
  232.       * If we needed to skip any extended headers, do so now, by
  233.       * reading extended headers and skipping past them in the 
  234.      * archive.
  235.      */
  236.     if (isextended) {
  237. /*        register union record *exhdr;
  238.  
  239.         for (;;) {
  240.             exhdr = findrec();
  241.  
  242.             if (!exhdr->ext_hdr.isextended) {
  243.                 userec(exhdr);
  244.                 break;
  245.             }
  246.             userec(exhdr);
  247.         }*/
  248.         skip_extended_headers();
  249.     }
  250.             
  251.     if(f_multivol)
  252.         save_name=head->header.name;
  253.     /* Skip to the next header on the archive */
  254.         
  255.     skip_file((long) hstat.st_size);
  256.         
  257.     if(f_multivol)
  258.         save_name = 0;
  259. }
  260.  
  261.  
  262. /*
  263.  * Read a record that's supposed to be a header record.
  264.  * Return its address in "head", and if it is good, the file's
  265.  * size in hstat.st_size.
  266.  *
  267.  * Return 1 for success, 0 if the checksum is bad, EOF on eof,
  268.  * 2 for a record full of zeros (EOF marker).
  269.  *
  270.  * You must always userec(head) to skip past the header which this
  271.  * routine reads.
  272.  */
  273. int
  274. read_header()
  275. {
  276.     register int    i;
  277.     register long    sum, recsum;
  278.     register char    *p;
  279.     register union record *header;
  280.     long    from_oct();
  281.  
  282.     header = findrec();
  283.     head = header;        /* This is our current header */
  284.     if (NULL == header)
  285.         return EOF;
  286.  
  287.     recsum = from_oct(8,  header->header.chksum);
  288.  
  289.     sum = 0;
  290.     p = header->charptr;
  291.     for (i = sizeof(*header); --i >= 0;) {
  292.         /*
  293.          * We can't use unsigned char here because of old compilers,
  294.          * e.g. V7.
  295.          */
  296.         sum += 0xFF & *p++;
  297.     }
  298.  
  299.     /* Adjust checksum to count the "chksum" field as blanks. */
  300.     for (i = sizeof(header->header.chksum); --i >= 0;)
  301.         sum -= 0xFF & header->header.chksum[i];
  302.     sum += ' '* sizeof header->header.chksum;    
  303.  
  304.     if (sum == recsum) {
  305.         /*
  306.          * Good record.  Decode file size and return.
  307.          */
  308.         if (header->header.linkflag == LF_LINK)
  309.             hstat.st_size = 0;    /* Links 0 size on tape */
  310.         else
  311.             hstat.st_size = from_oct(1+12, header->header.size);
  312.         return 1;
  313.     }
  314.  
  315.     if (sum == 8*' ') {
  316.         /*
  317.          * This is a zeroed record...whole record is 0's except
  318.          * for the 8 blanks we faked for the checksum field.
  319.          */
  320.         return 2;
  321.     }
  322.  
  323.     return 0;
  324. }
  325.  
  326.  
  327. /* 
  328.  * Decode things from a file header record into a "struct stat".
  329.  * Also set "*stdp" to !=0 or ==0 depending whether header record is "Unix
  330.  * Standard" tar format or regular old tar format.
  331.  *
  332.  * read_header() has already decoded the checksum and length, so we don't.
  333.  *
  334.  * If wantug != 0, we want the uid/